<template>
    <div class="imgList">
        <div style="font-size: 30px;font-weight: bolder;">图片列表
            <el-button type="text" @click="dialogVisible = true">上传图片</el-button>
        </div>
        <el-dialog title="上传图片" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
            <UploadImage />
        </el-dialog>
        <el-table :data="filesList" style="width: 90%" @click="getimgList">
            <el-table-column prop="imgName" label="图片名称" width="180">
            </el-table-column>
            <el-table-column prop="imgSize" label="图片大小" width="180">
            </el-table-column>
            <el-table-column prop="imgType" label="图片类型">
            </el-table-column>
            <el-table-column prop="filepath" label="图片缩略图">
                <template slot-scope="scope">
                    <el-image style="width: 50px;height: 50px;" :src="'http://localhost:3000/' + scope.row.imgName">
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button id="scope.row.imgId" type="danger" @click="delimgList(scope.row.imgId)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="block">
            <el-pagination @current-change="handleCurrentChange" :page-size="size" :current-page="currentPage"
                layout=" prev, pager, next" :total="length">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import UploadImage from '../components/UploadImage.vue';
import myaxios from '../components/axios';
export default {
    data() {
        return {
            dialogVisible: false,
            filesList: [],
            filesPage: [],
            length: 5,
            size: 5,
            currentPage: 1
        };
    },
    methods: {
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => {
                    done();
                    this.getimgList();
                })
                .catch(_ => { });

        },
        handleCurrentChange(val) {
            this.currentPage = val;
            console.log(this.length);
            this.getimgList();
        },
        async getimgList() {
            await myaxios.get("/imgList", { params: { size: this.size, page: this.currentPage } })
                .then(res => {
                    this.filesList = res.data.imgList
                    console.log(res.data);
                    this.length = res.data.length;
                })
        },
        async delimgList(id) {
            await myaxios.delete("/delImg" + id)
                .then(res => {
                    this.$message({
                        message: res.data.msg,
                        type: 'success'
                    });
                })
            this.getimgList();
        }
    },
    mounted() {
        this.getimgList();
    },
    components: { UploadImage }
}
</script>

<style>
.imgList {
    display: flex;
    justify-content: space-between;
    flex-direction: column;
    align-items: center;
}
</style>